package kit4j.tests.jdk.swing.dnd;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import javax.swing.table.TableCellRenderer;

import kit4j.tests.jdk.swing.SwingUtil;
import kit4j.tests.jdk.swing.dnd.component.JButtonBox;
import kit4j.tests.jdk.swing.dnd.component.JButtonTransferHandler;
import kit4j.tests.jdk.swing.dnd.component.SeparateTabbedPane;
import kit4j.tests.jdk.swing.dnd.factory.DragEnabledButtonFactory;
import kit4j.tests.jdk.swing.dnd.table.DndModelTableCellRenderer;
import kit4j.tests.jdk.swing.dnd.table.JTableTransferHandler;
import kit4j.view.cs.swing.dnd.ComponentMoveTransferHandler;
import kit4j.view.cs.swing.dnd.JComponentDragDisplayActivator;

/**
 * 测试Swing拖放效果。
 * 
 * @author MiniKnife
 * 
 */
public class TestDragAndDrop {

	public static void main(String[] args) {
		testDragButtonInMultiPane();
	}
	
	static void testDragButtonInMultiPane() {
		JPanel mainPane = new JPanel();
		Object[][] tableData = new Object[10][10];
		Object[] columnNames = new String[] {"列1", "列2", "列3", "列4", "列5", "列6", "列7", "列8", "列9", "列10"};
		JPanel tablePane = new SeparateTabbedPane();
		JScrollPane tableScrollPane = new JScrollPane();
		final TableCellRenderer tableRenderer = new DndModelTableCellRenderer();
		JTable table = new JTable(tableData, columnNames) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return !(getValueAt(row, column) instanceof JButton);
			}
			
			@Override
			public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
				return tableRenderer;
			}
		};
		JPanel carPane = new SeparateTabbedPane();
		JPanel panel1 = new JButtonBox(4, 4);
		JPanel panel2 = new JButtonBox(4, 4);
		JPanel panel3 = new JButtonBox(4, 4);
		JButton[] btns1 = DragEnabledButtonFactory.createUniqueButtons(9);
		JButton[] btns2 = DragEnabledButtonFactory.createUniqueButtons(5);
		JButton[] btns3 = DragEnabledButtonFactory.createUniqueButtons(8);
		
		mainPane.setLayout(new GridLayout(2, 1));
		tableScrollPane.setName("table");
		table.setFillsViewportHeight(true);
		table.setCellSelectionEnabled(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setDragEnabled(true);
		table.setTransferHandler(new JTableTransferHandler(table));
//		table.addMouseMotionListener(new JComponentDragDisplayActivator());
//		table.setSelectionBackground(Color.RED);
		panel1.setName("panel1");
		panel2.setName("panel2");
		panel3.setName("panel3");
		
		tableScrollPane.setViewportView(table);
		add(tablePane, tableScrollPane);
		add(panel1, btns1);
		add(panel2, btns2);
		add(panel3, btns3);
		add(carPane, panel1, panel2, panel3);
		add(mainPane, tablePane, carPane);
		
		SwingUtil.showComponent(mainPane);
//		SwingUtil.showComponent(table);
//		SwingUtil.showComponent(tableScrollPane);
//		SwingUtil.showComponent(tablePane);
	}
	
	static void add(Container container, Component... comps) {
		for(Component comp : comps) {
			container.add(comp);
		}
	}

	static void testDragButton() {
		final JPanel panelA = new JPanel();
		final JPanel panelB = new JPanel();
		SeparateTabbedPane tabbedPane = new SeparateTabbedPane();
		JButton btnA = new JButton("A");
		JButton btnB = new JButton("B");
		JButton btnC = new JButton("C");
		JButton btnD = new JButton("D");
		JButton repaintButton = new JButton("repaint");
		JTextField textField = new JTextField(20);
		JComponentDragDisplayActivator activator = new JComponentDragDisplayActivator();
		TransferHandler btnHandler = new JButtonTransferHandler();
		// ComponentMoveTransferHandler containerHandler = new
		// ComponentMoveTransferHandler();

		btnA.addMouseMotionListener(activator);
		btnA.setTransferHandler(btnHandler);
		btnA.setName("btnA");
		btnB.addMouseMotionListener(activator);
		btnB.setTransferHandler(btnHandler);
		btnB.setName("btnB");
		btnC.addMouseMotionListener(activator);
		btnC.setTransferHandler(btnHandler);
		btnC.setName("btnC");
		btnD.addMouseMotionListener(activator);
		btnD.setTransferHandler(btnHandler);
		btnD.setName("btnD");
		repaintButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent evt) {
				// panelA.paintAll(panelA.getGraphics());
				// panelB.paintAll(panelA.getGraphics());
				Container parent = panelA.getParent();
				// parent.repaint();
				parent.paintAll(parent.getGraphics());
				System.out.println("Repaint!");
			}
		});
		textField.setDragEnabled(true);

		panelA.setTransferHandler(new ComponentMoveTransferHandler(panelA));
		panelB.setTransferHandler(new ComponentMoveTransferHandler(panelB));

		panelA.setLayout(new FlowLayout());
		// panelA.add(btnA);
		// panelA.add(btnB);
		// panelA.add(btnC);
		tabbedPane.add(btnA);
		// tabbedPane.add(btnB);
		tabbedPane.add(btnC);

		panelB.setLayout(new FlowLayout());
		panelB.add(btnD);

		// SwingUtil.showComponent(panelA, new JButton("2"), panelB);
		SwingUtil.showComponent(tabbedPane);
		// SwingUtil.showComponent(textField, new JButton("0"));
	}
}
